package cn.crism.blog.service.impl;

import cn.crism.blog.entity.Article;
import cn.crism.blog.entity.Comment;
import cn.crism.blog.param.QueryParam;
import cn.crism.blog.repository.CommentRepository;
import cn.crism.blog.service.ArticleService;
import cn.crism.blog.service.CommentService;
import cn.crism.blog.service.ReplyService;
import cn.crism.blog.vo.CommentVO;
import cn.crism.blog.vo.ReplyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Crism
 * @Date: 上午9:19 18/2/7
 * @Description:
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    private ReplyService replyService;
    @Autowired
    private ArticleService articleService;
    @Override
    public Map<String, Object> getComments(QueryParam queryParam) {
        PageRequest pageable = new PageRequest(queryParam.getPage(), queryParam.getSize());
        Page<Comment> comments;
        if (queryParam.getParamId() == null) {
            comments = commentRepository.findAll(pageable);
        } else {
            comments = commentRepository.findAllByArticleIdAndCommentStatus(queryParam.getParamId(),true,pageable);
        }
        if (comments != null) {
            Map<String, Object> map = new HashMap<>();
            List<CommentVO> commentVOS = new ArrayList<>();
            if (queryParam.getParamId() == null) {
                for (Comment comment : comments) {
                    int count = replyService.getCount(comment);
                    String title = articleService.getAticleTitle(comment.getArticleId());
                    commentVOS.add(new CommentVO(comment,count,title));
                }
            } else {
                for (Comment comment : comments) {
                    List<ReplyVO> reply = replyService.getCommentReply(comment);
                    commentVOS.add(new CommentVO(comment,reply));
                }
            }
            map.put("totalElements", comments.getTotalElements());
            map.put("content", commentVOS);
            return map;
        }
        return null;
    }

    @Override
    public Boolean createComment(Comment comment) {
        try {
            commentRepository.save(comment);
        }catch (Exception e){
           return false;
        }finally {
            return true;
        }
    }

    @Override
    public Comment findCommentById(String commentId) {
        Comment comment = commentRepository.findOne(commentId);
        return comment;
    }

    @Override
    public Boolean accessComment(String commentId) {
        if (commentId!=null) {
            Comment comment = commentRepository.findOne(commentId);
            if (comment!=null){
                comment.setCommentStatus(true);
                commentRepository.save(comment);
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean removeComment(String commentId) {
        if (commentId!=null) {
            commentRepository.delete(commentId);
            return true;
        }
        return false;
    }

    @Override
    public Boolean removeComments(Integer articleId) {
        try {
            commentRepository.deleteCommentByArticleId(articleId);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
